<template>
    <div class="chargritems">
        <Topsearch :options='options' @send-value="getvalue"></Topsearch>
        <!-- 数据表格 -->
        <div class="datalists">
            <div class="datalistsname">数据列表</div>
            <!-- 添加按钮 -->
            <el-button type="primary" class="append" @click="append"><i class="el-icon-plus"></i>添加项目</el-button>
            <!-- 数据表格 -->
            <div class="datatable">
                <el-table
                :data="tableData"
                border
                stripe
                style="width: 100%">
                    <el-table-column
                    type="index"
                    label="序号"
                    width="55">
                    </el-table-column>
                    <el-table-column
                    prop="projectname"
                    label="项目名称"
                    width="190">
                    </el-table-column>
                    <el-table-column
                    prop="projecttype"
                    label="项目类别"
                    width="107">
                    </el-table-column>
                    <el-table-column
                    prop="Billingmethod"
                    label="计费方式"
                    width="191">
                    </el-table-column>
                    <el-table-column
                    prop="billingcycle"
                    label="账单周期"
                    width="133">
                    </el-table-column>
                    <el-table-column
                    prop="note"
                    label="备注"
                    width="133">
                    </el-table-column>
                    <el-table-column
                    prop="state"
                    label="状态"
                    width="155">
                    </el-table-column>
                    <el-table-column
                    prop="operate"
                    label="操作"
                    width="253">
                        <template slot-scope="scope">
                            <i class="el-icon-edit"></i>
                            <span style="margin-left: 5px;margin-right:5px" @click="edit(scope.row.items_id)">{{ scope.row.edit }}</span>
                            <i class="el-icon-delete"></i>
                            <span style="margin-left: 5px;margin-right:5px" @click="del(scope.row.items_id)">{{ scope.row.del }}</span>
                            <i class="el-icon-office-building"></i>
                            <span style="margin-left: 5px;margin-right:5px" @click="disable(scope.row)">{{ scope.row.Disable}}</span>
                        </template>
                    </el-table-column>
                </el-table> 
                <Pagecurren :totalpage="totalpage" :currentPage2="currentPage2" @send="getpage" @send-pagelimit="getpagelimit" :pagelimit="pagelimit"></Pagecurren>
            </div>
        </div>
    </div>
</template>

<script>
import Topsearch from '../components/Topsearch.vue'
import Pagecurren from '../components/Pagecurren.vue'
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {
    components:{
        Topsearch,
        Pagecurren
    },
    computed:{
        ...mapState(['num'])
    },
    watch:{
        currentPage2:{
            handler(val,oldval){
                this.tableData=this.datalists.slice((val-1)*this.pagelimit,val*this.pagelimit)
            },
        },
        pagelimit:{
            handler(val,oldval){
                this.tableData=this.datalists.slice((this.currentPage2-1)*val,val*this.currentPage2)
            }
        },
    },
    methods:{
        getpage(data){
            this.currentPage2=data
        },
        getpagelimit(data){
            this.pagelimit=data
        },
        getvalue(data){
            this.$axios.post('http://127.0.0.1/chargeitemsearch',{
                value:data
            },{
                headers: {
                    'Authorization': `Bearer ${localStorage.getItem('ticket')}`,
                    'Content-Type': 'application/json' // 如果需要发送 JSON 数据，则包含此行 // 设置请求头
                },
            }).then(res=>{
                this.datalists=res.data
                this.totalpage=res.data.length
                this.tableData=res.data.slice(0,10)
            }).catch(err=>{
                console.log(err)
            })
        },
        disable(txt){
            // console.log('禁用',txt)
            if(txt.state=="启用"){
                this.$axios.post('http://127.0.0.1/chargeitemdisable',{
                    value:'启用',
                    id:txt.items_id
                },{
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem('ticket')}`,
                        'Content-Type': 'application/json' // 如果需要发送 JSON 数据，则包含此行 // 设置请求头
                    },
                }).then(res=>{
                    this.datalists=res.data
                    this.totalpage=res.data.length
                    this.tableData=res.data.slice(0,10)
                    console.log(res)
                }).catch(err=>{
                    console.log(err)
                })
            }else{
                this.$axios.post('http://127.0.0.1/chargeitemdisable',{
                    value:'禁用',
                    id:txt.items_id
                },{
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem('ticket')}`,
                        'Content-Type': 'application/json' // 如果需要发送 JSON 数据，则包含此行 // 设置请求头
                    },
                }).then(res=>{
                    this.datalists=res.data
                    this.totalpage=res.data.length
                    this.tableData=res.data.slice(0,10)
                }).catch(err=>{
                    console.log(err)
                })
            }
            
        },
        edit(i){
            console.log('编辑',i)
        },
        del(i){
            console.log('删除',i)
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.post('http://127.0.0.1/chargeitemdel',{
                    id:i
                },{
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem('ticket')}`,
                        'Content-Type': 'application/json' // 如果需要发送 JSON 数据，则包含此行 // 设置请求头
                    },
                }).then(res=>{
                    this.datalists=res.data
                    this.totalpage=res.data.length
                    this.tableData=res.data.slice((this.currentPage2-1)*this.pagelimit,this.currentPage2*this.pagelimit)
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(err=>{
                    console.log(err)
                })
                
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
            
        },
        append(){
            console.log('添加')
            this.$router.push('/about/Addproject')
        },
        ...mapMutations(['changeTotalpage'])
    },
    data(){
        return{
            options: [{
            value: '选项1',
            label: '物业费'
            }, {
            value: '选项5',
            label: '车位管理费'
            }],
            // currentPage2:1,            
            // tableData:[
            //     {
            //         date: '1',
            //         projectname: '物业费',
            //         projecttype: '物业管理费用',
            //         Billingmethod:'按建筑面积',
            //         billingcycle:'1个月',
            //         note:'',
            //         state:'启用',
            //         edit:'编辑',
            //         del:'删除',
            //         Disable:'禁用'
            //     }, 
            //     {
            //         date: '2',
            //         projectname: '车位管理费',
            //         projecttype: '物业管理费用',
            //         Billingmethod:'按建筑面积',
            //         billingcycle:'1个月',
            //         note:'',
            //         state:'启用',
            //         edit:'编辑',
            //         del:'删除',
            //         Disable:'禁用'
            //     }, 
            //     {
            //         date: '3',
            //         projectname: '车位管理费',
            //         projecttype: '物业管理费用',
            //         Billingmethod:'按建筑面积',
            //         billingcycle:'1个月',
            //         note:'',
            //         state:'启用',
            //         edit:'编辑',
            //         del:'删除',
            //         Disable:'禁用'
            //     }, 
            //     {
            //         date: '4',
            //         projectname: '车位管理费',
            //         projecttype: '物业管理费用',
            //         Billingmethod:'按建筑面积',
            //         billingcycle:'1个月',
            //         note:'',
            //         state:'启用',
            //         edit:'编辑',
            //         del:'删除',
            //         Disable:'禁用'
            //     }, 
            // ],
            totalpage:300,
            currentPage2:1,
            pagelimit:10,
            tableData:[],
            datalists:[],
            
        }
    },
    mounted(){
        this.$axios.get('http://127.0.0.1/chargeitems',{
            headers: {
                'Authorization': `Bearer ${localStorage.getItem('ticket')}`,
                'Content-Type': 'application/json' // 如果需要发送 JSON 数据，则包含此行 // 设置请求头
            },
        }).then(res=>{
            this.datalists=res.data
            this.totalpage=res.data.length
            this.tableData=res.data.slice(0,10)
        }).catch(err=>{
            console.log(err)
        })
    }
}
</script>

<style lang="scss" scoped>
    .chargritems{
        width: 1260px;
        margin: auto;
        height: 80vh;
        overflow: scroll;
        scrollbar-width: none;
        // 数据列表
        .datalists{
            width: 1260px;
            height: 1196px;
            padding: 0 20px;
            margin-top: 10px;
            background-color: #fff;
            border: 1px solid rgba(233, 233, 233, 1);
            position: relative;
            box-sizing: border-box;
            // 数据列表名字名称
            .datalistsname{
                margin-top: 40px;
                width: 57px;
                height: 18px;
                line-height: 18px;
                color: #666666;
                font-size: 14px;
            }
            // 添加数据按钮
            .append,.appends{
                min-width: 80px;
                padding: 0 5px;
                height: 35px;
                position: absolute;
                right: 20px;
                top: 23px;
                display: flex;
                align-items: center;
                justify-content: center;
                box-sizing: border-box;
                background-color: #0079fe !important;
                .el-icon-plus,.el-icon-folder-add{
                    font-size: 16px;
                    margin-right: 5px;
                    font-weight: 700;
                }
            }
            .append:hover,.appends:hover{
                background-color: #66b1ff !important;
                color: #fff !important;
            }
            .appends{
                position: absolute;
                padding: 0 2px;
                right: 120px;
                top: 23px;
            }
            // 数据表格
            .datatable{
                margin-top: 20px;
                ::v-deep .has-gutter th{
                    height: 45px !important;
                    background-color: #f5f5f5;
                    box-sizing: border-box;
                    padding: 0 !important;
                    font-size: 14px;
                    color: #666666;
                    font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑' !important;
                }
                ::v-deep .el-table__row td{
                    height: 50px !important;
                    padding: 0 !important;
                }
                ::v-deep .el-table--enable-row-hover .el-table__body tr:hover>td.el-table__cell {
                    background-color: #f9f9f9;
                }
                ::v-deep .el-table__row .el-table__cell:nth-child(2){
                    color: #0079fe !important;
                }
                ::v-deep .el-table__row .el-table__cell:nth-child(1){
                    color: #666 !important;
                    text-align: center !important;
                }
                ::v-deep .el-table__row .el-table__cell:nth-child(4){
                    color: #999 !important;
                }
                ::v-deep .el-table__row .el-table__cell:nth-last-child(1){
                    text-align: center;
                    color: #0079fe;
                    // cursor: pointer;
                }
                ::v-deep .el-table__row .el-table__cell:nth-last-child(1) span:hover{
                    cursor: pointer;
                    text-decoration: underline !important;
                    // color: #4a4a4a;
                }
                ::v-deep .el-table__row .el-table__cell{
                    color: #666666;
                    font-size: 12px;
                }
                ::v-deep .el-table__row:hover{
                    background-color: inherit !important;
                }
                
            }
        }
    }
</style>